package com.hmall.feign.controller;

import com.hmall.feign.client.ItemClient;
import com.hmall.feign.dto.Item;
import com.hmall.feign.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Resource
    private ItemClient itemClient;

    @Value("${server.port}")
    private int port;

    @GetMapping("/list")
    public PageDTO<Item> queryItemByPage(@RequestParam("page") Integer page,
                                         @RequestParam("size") Integer size) {
        log.info("feign-service port is => {}", port);
        return itemClient.queryItemByPage(page, size);
    }

    @GetMapping("/{id}")
    public Item queryItemById(@PathVariable("id") Long id) {
        return itemClient.queryItemById(id);
    }

    @PostMapping
    public void saveItem(@RequestBody Item item) {
        itemClient.saveItem(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        itemClient.updateItemStatus(id, status);
    }

    @PutMapping
    public void updateItem(@RequestBody Item item) {
        itemClient.updateItem(item);
    }

    @DeleteMapping("{id}")
    public void deleteItemById(@PathVariable("id") Long id) {
        itemClient.deleteItemById(id);
    }
}
